﻿using Harmontronics.Api.Config;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Business;
using Harmontronics.Model.Dto.Login;
using Harmontronics.Model.Dto.User;
using Harmontronics.Model.Entitys;
using Harmontronics.Model.Other;
using Harmontronics.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using Org.BouncyCastle.Crypto;

namespace Harmontronics.Api.Controllers
{
    [Route("api/[controller]/[action]")]
   // [Authorize]
    [ApiController]
    public class ProductionLineController : ControllerBase
    {
        private IProductionLineService _productionLineService;

        public ProductionLineController(IProductionLineService productionLineService)
        {
            _productionLineService = productionLineService;
        }
        [HttpGet]
        public ApiResponseData GetAllProductionLineData()
        {
            
            List<ProductionLine> models = _productionLineService.GetAllProductionLine();
            return ResultHelper.Success(new ReturnData() { total = models.Count, dataList = models });
        }
        //获取全部数据
        [HttpPost]
        public ApiResponseData GetProductionLineData(ProductionLineInput input)
        {
            List<ProductionLine> entitiesList = _productionLineService.GetAllProductionLine(); 
            if (!input.ProductionLineId.IsNullOrEmpty())
            {
                entitiesList = entitiesList.Where(i =>i.ProductionLineId.Contains(input.ProductionLineId)).ToList();
            }
            if(!input.WorkShopId.IsNullOrEmpty())
            {
                entitiesList = entitiesList.Where(i => i.WorkShopId.Contains(input.WorkShopId)).ToList();
            }

            int count = entitiesList.Count;
            if (!entitiesList.IsNullOrEmpty())
            {
                int start = (input.page - 1) * input.size;
                int end = ((start + input.size) >= (entitiesList.Count)) ? (entitiesList.Count) : (start + input.size);
                entitiesList = entitiesList.GetRange(start, end - start);
            }
            List<ProductionLineModel> models = _productionLineService.MendingRange(entitiesList);
            return ResultHelper.Success(new ReturnData() { total = count, dataList = models });
        }
        [HttpPost]
        public ApiResponseData AddProductionLine(ProductionLine model)
        {
            //重复性判断
            if(_productionLineService.GetProductionLineById(model.ProductionLineId) != null)
            {
                return ResultHelper.Error(502, "ID cannot be repeated! ");
            }
            if (_productionLineService.AddProductionLine(model))
            {
                return ResultHelper.Success("Add successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to add!");
            }
        }
        [HttpPost]
        public ApiResponseData UpdateProductionLine(ProductionLine model)
        {
            //空判断
            if (_productionLineService.GetProductionLineById(model.ProductionLineId) == null)
            {
                return ResultHelper.Error(502, "There is no data with Id " + model.ProductionLineId + "!");
            }
            if (_productionLineService.UpdateProductionLine(model))
            {
                return ResultHelper.Success("Update successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to Update!");
            }
        }
        [HttpPost]
        public ApiResponseData DeleteProductionLine(DeleteStringId id)
        {
            if (_productionLineService.GetProductionLineById(id.id) == null)
            {
                return ResultHelper.Error(502, "There is no data with Id " + id + "!");
            }
            if (_productionLineService.DeleteProductionLine(id.id))
            {
                return ResultHelper.Success("Delete successfully!");
            }
            else
            {
                return ResultHelper.Error(500, "Failed to Delete!");
            }
        }
        [HttpPost]
        public ApiResponseData BatchDelProductionLine(List<string> ids)
        {
            if (ids.Count > 0)
            {
                if (_productionLineService.BatchDelProductionLine(ids))
                {
                    return ResultHelper.Success("Delete successfully!");
                }
                else
                {
                    return ResultHelper.Error(500, "Failed to Delete!");
                }
            }
            return ResultHelper.Error(502, "No data detected!");
        }

    }
}
